const https = require("https");
let _get = (url)=>{
  return new Promise((resolve,reject)=>{
    var _request = https.get(url,(ret)=>{
      var datas = [];
      var size = 0;
      ret.on('data', function (data) {
        datas.push(data);
        size += data.length;
        //process.stdout.write(data);  
      });
      ret.on("end", function () {
        var buff = Buffer.concat(datas, size);
        var result = buff.toString();//不需要转编码,直接tostring  
        resolve(result);
      }); 
    }).on("error", (error) => {
      reject(error);
    })
    _request.end();
   })
 }

let _post = (host,url,data) => {
  var data = JSON.stringify(data) || "";
  var options = {
    hostname: host,
    port: 443,
    path: url,
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': data.length
    }
  };
  return new Promise((resolve, reject) => {
    var _request = https.request(options, (ret) => {
      var datas = [];
      var size = 0;
      ret.on('data', function (data) {
        datas.push(data);
        size += data.length;
        //process.stdout.write(data);  
      });
      ret.on("end", function () {
        var buff = Buffer.concat(datas, size);
        // var result = buff.toString();//不需要转编码,直接tostring  
        resolve(buff);
      });
    }).on("error", (error) => {
      reject(error);
      _request.end();
    })
    _request.write(data);
    _request.end();
  })
}

 module.exports ={
   get:_get,
   post:_post
 }